package com.hhkj.admin.bank.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hhkj.admin.bank.common.cont.BankConst;
import com.hhkj.admin.bank.common.mybatis.handler.TestAmountConfMapHandler;
import com.hhkj.admin.bank.common.valid.*;
import com.hhkj.admin.bank.model.extend.TestQuestionAmountConf;
import com.hhkj.admin.bank.model.req.BankReq;
import com.hhkj.admin.course.common.mybatis.handler.StrIntMapHandler;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.core.domain.BizBaseEntity;
import com.ruoyi.common.valid.VAdd;
import com.ruoyi.common.valid.VEdit;
import com.ruoyi.common.valid.emun.EnumValueCheck;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题库信息对象 t_bank
 *
 * @author ruoyi
 * @since 2023-05-16
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("t_bank")
@AllArgsConstructor
@NoArgsConstructor
public class Bank extends BizBaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 名称 */
    @Excel(name = "名称")
    @NotBlank(message = "名称不能为空", groups = {VEditName.class, VPublishBank.class})
    private String name;

    /** 类型编码 全题库:total、练习题库:practise、测试题库:test */
    @NotNull(message = "题库类型不能为空", groups = {VListAllCoursePublishOfRule.class})
    private String typeCode;

    /** 上级题库id */
    private Long parentId;

    /** 课程id 关联 t_course 表的 id 字段 */
    @Excel(name = "课程id 关联 t_course 表的 id 字段")
    private Long courseId;

    /** 科目名称 */
    @TableField(exist = false)
    private String courseName;

    /** 上级课程id信息 */
    @NotNull(message = "类别信息不能为空", groups = {VAdd.class, VDownloadQuestionImportTemplate.class})
    private Long courseParentId;

    /** 课程题库类型id 关联 t_course_bank_type 表的 id 字段 */
    @Excel(name = "课程题库类型id 关联 t_course_bank_type 表的 id 字段")
    @NotNull(message = "题库类型不能为空", groups = {VAdd.class})
    private Long courseBankTypeId;

    /** 题库类型名称 */
    @TableField(exist = false)
    private String courseBankTypeName;

    /** 发布标签 1-是 0-否 */
    @Excel(name = "发布标签 1-是 0-否")
    @EnumValueCheck(message = "发布标签数值错误 是：1、否：0", groups = {VEditPublishFlag.class})
    private Integer publishFlag;

    /** 题库题目类型数量 { 题库题目组数量信息 { "single": 100, "multiple": 100, "judge": 100, "case": 100 } } */
    @Excel(name = "题库题目类型数量")
    @TableField(typeHandler = StrIntMapHandler.class)
    private Map<String, Integer> typeAmountConf;

    /** 测试题目数量配置{\"single\":{\"total\":12,\"practice\":12,\"residue\":12}} */
    @TableField(typeHandler = TestAmountConfMapHandler.class)
    private Map<String, TestQuestionAmountConf> testAmountConfMap = new HashMap<>();

    /** 案例题每组题目数量 */
    @Excel(name = "案例题每组题目数量")
    private Integer caseGroupSize;

    /** 题库题目数量 */
    private Integer questionAmount;

    /** 规则id 关联 t_bank_rule 的 id 字段信息 */
    @NotNull(message = "规则信息不能为空", groups = {VListAllCoursePublishOfRule.class})
    private Long ruleId;

    /** 备注信息 */
    private String remark;

    /** excel文件信息 */
//    @JsonIgnore
    @TableField(exist = false)
    @NotNull(message = "导入文件不能为空", groups = {VAdd.class})
    private MultipartFile file;

    /** 创建时间范围开始时间结点 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @TableField(exist = false)
    private Date createTimeStart;

    /** 创建时间范围结束时间结点 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @TableField(exist = false)
    private Date createTimeEnd;

    /** 题库规则信息 */
    @TableField(exist = false)
    private BankRule rule;

    /** 科目id列表 */
    @TableField(exist = false)
    private List<Long> courseIdList;

    /** 规则id列表 */
    @TableField(exist = false)
    private List<Long> ruleIdList;

    public BankReq createChild(BankConst.Type type, Long ruleId)
    {
        BankReq child = new BankReq();
        child.setName(this.getName() + type.getName());
        child.setTypeCode(type.getCode());
        child.setParentId(this.getId());
        child.setCourseId(this.getCourseId());
        child.setCourseParentId(this.getCourseParentId());
        child.setCourseBankTypeId(this.getCourseBankTypeId());
        child.setCaseGroupSize(this.getCaseGroupSize());
        child.setRuleId(ruleId);
        child.setPublishFlag(SysConst.Flag.IS.getFlag());
        return child;
    }

    @JsonIgnore(false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    public Date getCreateTime()
    {
        return super.getCreateTime();
    }

    @Override
    @NotNull(message = "唯一主键不能为空", groups = {VEdit.class, VEditName.class, VEditPublishFlag.class})
    public Long getId()
    {
        return super.getId();
    }

    public void setRule(BankRule rule)
    {
        this.rule = rule;
    }


}
